package com.pe.pgn.clubpgn.webapp.controller.incremental;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpbIncremental;
import com.pe.pgn.clubpgn.domain.ClpbPrograma;
import com.pe.pgn.clubpgn.domain.ClpdPremioIncremental;
import com.pe.pgn.clubpgn.domain.beans.BNIncremental;
import com.pe.pgn.clubpgn.domain.beans.BNPremioIncremental;
import com.pe.pgn.clubpgn.service.IncrementalService;
import com.pe.pgn.clubpgn.service.ProgramaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/incremental/**")
public class IncrementalController extends BaseFormController {

	@Autowired
	private IncrementalService incrementalService;
	
	@Autowired
	private ProgramaService programaService;
	
	@RequestMapping(value = "/incrementallist.html")
	public @ModelAttribute("incremental") BNIncremental buscarIncrementales(
			@ModelAttribute("incremental") BNIncremental incremental, 
			@RequestParam(value = "search", required = false) String search, Model model){
		
		List<ClpbPrograma> programas = programaService.findListaProgramas();
		model.addAttribute("programas", programas);
		
		if(StringUtils.isBlank(search)){
			
			List<BNIncremental> incrementales = incrementalService.buscarIncrementales(null, null, null, Boolean.TRUE);
			model.addAttribute("incrementales", incrementales);			
			return new BNIncremental();
		}
		
		List<BNIncremental> incrementales = incrementalService.buscarIncrementales(new Long(incremental.getCoPrograma().toString()), 
						incremental.getDeIncremental(), incremental.getStIncremental(), Boolean.FALSE);
		model.addAttribute("incrementales", incrementales);		
		return incremental;
	}
	
	@RequestMapping(value = "/incrementalform.html", method = RequestMethod.GET)
	public @ModelAttribute("incremental") ClpbIncremental verIncremental(
			@RequestParam(value = "id", required = false) Long id, Model model){
		
		if(id != null){
			
			ClpbIncremental incremental = incrementalService.obtenerIncremental(id);
			return incremental;			
		}
		
		List<ClpbPrograma> programas = programaService.findListaProgramas();
		model.addAttribute("programas", programas);
		
		ClpbIncremental incremental = new ClpbIncremental();
		incremental.setStIncremental(Boolean.TRUE);
		
		return incremental;		
	}	
	
	@RequestMapping(value = "/incrementalform.html", method = RequestMethod.POST)
	public String guardarIncremental(@ModelAttribute("incremental") ClpbIncremental incremental,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {
			
			if (result.hasErrors()) {
				model.addAttribute("incremental", incremental);
				return "/incremental/incrementalform";
			}
			
			incrementalService.guardarIncremental(incremental);
			saveMessage(request, getText("incrementalform.added", incremental.getDeIncremental(), 
					request.getLocale()));
			return "redirect:incrementallist.html";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/incremental/incrementalform";
		}
	}	
	
	@RequestMapping(value = "/eliminarincremental.html")
	public String eliminarIncremental(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request) {

		try {

			incrementalService.eliminarIncremental(id);
			saveMessage(request, getText("incrementalform.deleted", request.getLocale()));
			return "redirect:incrementallist.html";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/incremental/incrementalform";
		}
	}
	
	@RequestMapping(value = "/premioincrementalform.html", method = RequestMethod.GET)
	public @ModelAttribute("premioIncremental") BNPremioIncremental verPremioIncremental(
			@RequestParam(value = "id", required = false) Long id, Model model){
		
		List<ClpdPremioIncremental> premios = incrementalService.obtenerPremiosPorIncremental(id);
		model.addAttribute("premios", premios);
		
		BNPremioIncremental premioIncremental = incrementalService.obtenerDatosIncremental(id);
		premioIncremental.setId(BigDecimal.ONE.negate());
		return premioIncremental;
	}	
	
	@RequestMapping(value = "/premioincrementalform.html", method = RequestMethod.POST)
	public String guardarPremioIncremental(@ModelAttribute("premioIncremental") BNPremioIncremental premioIncremental,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {
			
			boolean esDescripcionPremioRepetido = incrementalService.esDescripcionPremioRepetido(premioIncremental);
			if(esDescripcionPremioRepetido){
				saveError(request, getText("premioincrementalform.es.descripcion.repetida", request.getLocale()));
				List<ClpdPremioIncremental> premios = incrementalService.obtenerPremiosPorIncremental(new Long(premioIncremental.getCoIncremental().toString()));
				model.addAttribute("premios", premios);
				return "/incremental/premio/premioincrementalform";
			}
			
			boolean esValorPremioRepetido = incrementalService.esValorPremioRepetido(premioIncremental);
			if(esValorPremioRepetido){
				saveError(request, getText("premioincrementalform.es.valor.repetido", request.getLocale()));
				List<ClpdPremioIncremental> premios = incrementalService.obtenerPremiosPorIncremental(new Long(premioIncremental.getCoIncremental().toString()));
				model.addAttribute("premios", premios);
				return "/incremental/premio/premioincrementalform";
			}
			
			if (result.hasErrors()) {
				model.addAttribute("premioIncremental", premioIncremental);
				return "/incremental/premio/premioincrementalform";
			}
			
			incrementalService.guardarPremioIncremental(premioIncremental);
			saveMessage(request, getText("premioincrementalform.added", premioIncremental.getDePremioIncremental(), 
					request.getLocale()));
			return "redirect:premioincrementalform.html?id=" + premioIncremental.getCoIncremental();
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "redirect:premioincrementalform.html?id=" + premioIncremental.getCoIncremental();
		}
	}	
	
	@RequestMapping(value = "/eliminarpremioincremental.html")
	public String eliminarPremioIncremental(@RequestParam(value = "id", required = false) BigDecimal id, 
			HttpServletRequest request) {

		try {

			Long coIncremental = incrementalService.eliminarPremio(id);
			return "redirect:premioincrementalform.html?id=" + coIncremental;
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "redirect:premioincrementalform.html?id=" + id;
		}
	}	
	
}
